package org.nobody.mallb2c.mallmember.controller;

import java.util.Arrays;
import java.util.Map;

import org.nobody.common.exception.ResultCodeEnum;
import org.nobody.mallb2c.mallmember.exception.PhoneExistException;
import org.nobody.mallb2c.mallmember.exception.UsernameExistException;
import org.nobody.mallb2c.mallmember.vo.MemberLoginVo;
import org.nobody.mallb2c.mallmember.vo.MemberRegistVo;
import org.nobody.mallb2c.mallmember.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallmember.entity.MemberEntity;
import org.nobody.mallb2c.mallmember.service.MemberService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;



/**
 * 会员
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 16:05:29
 */
@RestController
@RequestMapping("mallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

  @PostMapping("/oauth2/login")
  public R oauthLogin(@RequestBody SocialUser vo){
    MemberEntity entity = memberService.login(vo);
    if (entity != null) {
      return R.ok().setData(entity);
    }else{
      return R.error(ResultCodeEnum.SOCIAL_LOGIN_INVALID_EXCEPTION.getCode(),ResultCodeEnum.SOCIAL_LOGIN_INVALID_EXCEPTION.getMsg());
    }
  }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
      try {
        memberService.regist(vo);
      } catch (PhoneExistException e) {
        return R.error(ResultCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),ResultCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
      } catch (UsernameExistException e) {
        return R.error(ResultCodeEnum.USER_EXIST_EXCEPTION.getCode(),ResultCodeEnum.USER_EXIST_EXCEPTION.getMsg());
      }

      return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
      MemberEntity entity = memberService.login(vo);
      if (entity != null) {
        return R.ok().setData(entity);
      }else{
        return R.error(ResultCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(),ResultCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
      }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("mallmember:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("mallmember:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallmember:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("mallmember:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("mallmember:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
